package com.scedu.dao;


import com.scedu.entity.Department;
import com.scedu.entity.Employee;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

//员工dao
@Repository
public class EmployeeDao {
    //模拟数据库中的数据
    private static Map<Integer, Employee> employeeMap=null;
    //员工有所属的部门
    @Autowired
    private DepartmentDao departmentDao;
    static {
        employeeMap =new HashMap<>();
        employeeMap.put(1, new Employee(1,"张三","123@qq.com",0,new Department(101,"生产部")));
        employeeMap.put(2, new Employee(2,"李四","12322@qq.com",1,new Department(101,"生产部")));
        employeeMap.put(3, new Employee(3,"张三3","12333@qq.com",0,new Department(101,"生产部")));
        employeeMap.put(4, new Employee(4,"王五","4567@163.com",1,new Department(102,"销售部")));
    }
    //员工操作 增删改查

    //主键自增
    private static Integer initId=5;
    //增加一个员工
    public void add(Employee employee){
        if(employee.getId()==null){
            employee.setId(initId++);
        }
        employee.setDepartment(departmentDao.getDepartmentById(employee.getDepartment().getId()));
        employeeMap.put(employee.getId(),employee);
    }
    //查询全部员工信息
    public Collection<Employee> getall(){
        return employeeMap.values();
    }
    //通过id查询员工信息
    public Employee getEmployeeById(Integer id){
        return employeeMap.get(id);
    }
    //删除
    public void delEmployeeById(Integer id){
        employeeMap.remove(id);
    }

}
